﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using com.subb.WebFramework;
using WebEntities;
using WebEntities.Models.Framework;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class MenuController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllMenu(StoreRequestParameters parms)
        {
            FW_MenuDAL mdal = new FW_MenuDAL();
            int ttlCount;
            List<FW_Menu> dbmenulist = mdal.GetAllMenus(parms.Start, parms.Limit, out ttlCount);
            List<UIMenu> menulist = new List<UIMenu>();
            foreach (var menu in dbmenulist)
            {
                UIMenu uimenu = UIMenu.AsUIMenu(menu);
                menulist.Add(uimenu);
            }
            return this.Store(menulist, ttlCount);
        }

        public ActionResult GetAllMenuByMtype()
        {
            FW_MenuDAL mdal = new FW_MenuDAL();
            List<FW_Menu> dbmenulist = mdal.GetAllMenusByType(UIMenu.BackMenuType);
            List<UIMenu> menulist = new List<UIMenu>();
            foreach (var menu in dbmenulist)
            {
                UIMenu uimenu = UIMenu.AsUIMenu(menu);
                menulist.Add(uimenu);
            }
            return this.Store(menulist);
        }

        [HttpPost]
        public ActionResult DeleteMenu(List<UIMenu> delMenu)
        {
            try
            {
                FW_MenuDAL mdal = new FW_MenuDAL();
                foreach (var menu in delMenu)
                {
                    mdal.RemoveMenu(menu.MenuID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewMenuWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建菜单",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateMenu"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateMenu()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateMenu(UIMenu menu)
        {
            //新建菜单
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MenuDAL mdal = new FW_MenuDAL();
                mdal.CreateMenu(menu.MenuName, menu.MenuType, menu.SortNum, menu.MenuUrl);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "菜单创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditMenuWindow(int menuId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑菜单",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutEdit,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditMenu"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("menuId", menuId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditMenu(int menuId)
        {
            FW_MenuDAL mdal = new FW_MenuDAL();
            FW_Menu dbmenu = mdal.GetMenu(menuId);
            UIMenu menu = UIMenu.AsUIMenu(dbmenu);
            return View(menu);
        }

        [HttpPost]
        public ActionResult EditMenu(UIMenu menu)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MenuDAL mdal = new FW_MenuDAL();
                mdal.UpdateMenu(menu.MenuID, menu.MenuName, menu.MenuType, menu.SortNum, menu.MenuUrl);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "菜单更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
